// import create from 'dd-store'
// import pageStore from '../../../../stores/globalStore'
import { selectMyPmById, getPerformData, selfScoreApi } from "../../../../api/manage"

Page({
  data: {
    // 路由数据
    routeData: {},
    // 绩效基本信息
    pmBaseInfo: {},
    // 当前项标识
    currentItemNu: 1,
    // 绩效数据
    pmData: [],
    // 当前是自评还是上级评分的标识
    scoreType: '',
    isShowTestBox: false,
    // 评分总结
    scoreSumUp: '',
    // 总分
    sumScore: 0
  },
  onLoad(options) {
    console.log('评分页面接收数据', options);
    this.setData({
      routeData: options,
      scoreType: options.scoreType
    })
    this.selectPmBaseInfoFunc()
    this.getPerformDataFunc()

  },
  // 判断是自评还是上级评分的函数
  judgeScoreTypeFunc(makeUserDDId, userId) {
    console.log('操作id', makeUserDDId);
    console.log('用户id', userId);
    let type
    makeUserDDId == userId ? (type = 'zp') : (type = 'sjpf')
    console.log('评分类型', type);
    return type
  },
  // 总评分计算函数
  sumScoreCompFunc() {
    let pD = this.data.pmData
    let nSumScore = 0
    pD.forEach((item) => {
      let weightC = item.pmTargetWeight / 100
      nSumScore += (item.score * weightC)
    })

    this.setData({
      sumScore: Number(nSumScore.toFixed(1))
    })
  },
  // 评分输入框change事件
  scoreInputChange(e) {
    console.log('评分输入框Change', e);
    let newData = this.data.pmData
    newData.forEach((item, index) => {
      if (this.data.currentItemNu - 1 == index) {
        item.score = e
      }
    })
    this.setData({
      pmData: newData
    })
    this.sumScoreCompFunc()
  },


  // 评分说明输入框失焦事件
  scoreDescTextChange(e) {
    console.log('评分说明失焦', e);
    let newData = this.data.pmData
    newData.forEach((item, index) => {
      if (this.data.currentItemNu - 1 == index) {
        item.scoreContent = e
      }
    })
    this.setData({
      pmData: newData
    })
  },

  // 评分总结框失焦函数
  scoreSumUpTextChange(e) {
    console.log('评分总结失焦', e);
    this.setData({
      scoreSumUp: e
    })
  },
  // 查询绩效基本信息的函数
  async selectPmBaseInfoFunc() {
    let data = {
      pmId: this.data.routeData.pmId
    }
    await selectMyPmById(data).then((res) => {
      console.log('绩效基本信息', res);
      this.setData({
        pmBaseInfo: res.data
      })
    })
  },

  // 查询绩效数据的函数
  async getPerformDataFunc() {
    dd.showLoading({
      content: "加载中"
    })
    let data = {
      pmId: this.data.routeData.pmId
    }
    await getPerformData(data).then((res) => {
      console.log('当前绩效数据', res);
      let newPmData = []
      // 遍历维度
      let indexs = 1
      res.data.forEach((itemW) => {
        // 遍历指标
        itemW.pmTargetList.forEach((itemT, index) => {
          console.log(index);
          let pmObj = {
            // 
            scoreContent: '',
            pmTargetName: itemT.pmTargetName,
            // scoreContent: itemT.pmTargetStand,
            pmDimensionName: itemW.pmDimensionName,
            pmDimensionWeight: itemW.pmDimensionWeight,
            pmTargetWeight: itemT.pmTargetWeight,
            pmTargetStand: itemT.pmTargetStand,
            pmTargetId: itemT.pmTargetId,
            pmProgressId: Number(this.data.routeData.pmProgressId),
            makeUserName: this.data.pmBaseInfo.pmUserName,
            makeUserDDId: this.data.routeData.userId,
            score: 0,
            scoreDesc: itemT.selfContentTem ? itemT.selfContentTem : '',
            nu: indexs,
          }
          indexs++
          newPmData.push(pmObj)
        })
      })
      console.log('整理指标数据', newPmData);
      this.setData({
        pmData: newPmData
      })
      dd.hideLoading()
    }).catch((err) => {
      dd.hideLoading()
      dd.showToast({
        content: "加载失败",
        type: "fail",
        duration: 1000
      })
    })
  },

  // 上一项点击事件
  previousClick() {
    console.log('上一项');
    this.setData({
      currentItemNu: this.data.currentItemNu - 1
    })
  },
  // 下一项点击事件
  nextItemClick() {
    let tData = this.data.pmData
    tData.forEach((item, index) => {
      if (this.data.currentItemNu - 1 == index) {
        console.log('当前项标识', this.data.currentItemNu);
        console.log('当前项分数', item.score);
        if (item.score != "" && item.score != 0) {
          setTimeout(() => {
            console.log('下一项');
            this.setData({
              currentItemNu: this.data.currentItemNu + 1
            })
          }, 100)
        } else {
          console.log(1111);
          dd.showToast({
            content: "请填写评分",
            type: "fail",
            duration: 1500
          })
        }
      }
    })

  },
  // 提交评分点击事件
  async submitScoreClick() {
    let nPmData = []
    let oPmData = this.data.pmData
    let submitFlag = true
    for (const item of oPmData) {
      console.log(item.score);
      if (item.score == null || item.score == undefined) {
        console.log('进来了');
        submitFlag = false
        dd.showToast({
          content: `请填写${item.pmTargetName}的评分`,
          type: "fail",
          duration: 1500
        })
        // break;
      }
      let nObj = {
        score: item.score,
        scoreContent: item.scoreContent,
        pmProgressId: Number(this.data.routeData.pmProgressId),
        makeUserName: item.makeUserName,
        makeUserDDId: item.makeUserDDId,
        scoreDesc: item.scoreDesc,
        pmTargetId: item.pmTargetId
      }
      nPmData.push(nObj)
    }

    if (submitFlag) {
      let data = {
        pmProgressId: Number(this.data.routeData.pmProgressId),
        makeType: 'submit',
        data: JSON.stringify(nPmData),
        scoreSummary: this.data.scoreSumUp
      }
      dd.showLoading({
        content: "提交中"
      })
      await selfScoreApi(data).then((res) => {
        if (res.result == "success") {

          dd.navigateBack({
            delta: 3
          })
          setTimeout(() => {
            dd.showToast({
              content: "提交成功",
              type: "success",
              duration: 1000
            })
          }, 200);
        }
      }).finally(() => {
        dd.hideLoading()
      })
    }

  }
});
